#! /usr/bin/env fan
//
// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-04-10  Jed Young  Creation
//

using gfx
using fwt

**
** Convert java or C# code to Fantom
**
class CodeTranslate
{
  Void main()
  {
    initJavaMap
    initCshapMap

    Window
    {
      title = "Code Translate"
      size = Size(900,500)

      srcText := Text{ multiLine = true; font = Desktop.sysFontMonospace }
      tagText := Text{ multiLine = true; font = Desktop.sysFontMonospace }

      jbtn := Button
      {
         text = "Java => fantom"
         onAction.add
         {
            s := javaTranslate(srcText.text)
            tagText.text = s
         }

      }
      cbtn := Button
      {
        text = "C# => fantom"
        onAction.add
        {
          s := cshapTranslate(srcText.text)
          tagText.text = s
        }
      }

       EdgePane
       {
          center = InsetPane { srcText, }
          right = InsetPane
          {
            GridPane
            {
              halignCells = Halign.fill
              jbtn,
              cbtn,
              Button{ text = "clear"; onAction.add { srcText.text = ""; tagText.text = "" }},
              Button{ text = "copyResult"; onAction.add { tagText.selectAll; tagText.copy }},
            },
          }
       },
       InsetPane { tagText, },

    }.open
  }

  Str:Str javaMap := [Str:Str][:] { ordered = true }
  Void initJavaMap()
  {
     javaMap["\r\n"]        = "\n"
     javaMap["Object "]     = "Obj "
     javaMap["String "]     = "Str "
     javaMap["int "]        = "Int "
     javaMap["long "]       = "Int "
     javaMap["double "]     = "Float "
     javaMap["float "]      = "Float "
     javaMap["boolean "]    = "Bool "
     javaMap["void "]       = "Void "
     javaMap[" final "]       = " const "

     javaMap["toString()"]  = "toStr"
     javaMap[" extends "]   = " : "
     javaMap[" impleaments "] = " : "
     javaMap[" instansof "] = " is "

     javaMap["@Override\n"] = "override"
     javaMap["@Override"]   = "override"
     javaMap["override  "]   = "override "

     javaMap["public "]     = ""
     javaMap[" new "]       = " "
     javaMap[";"]           = ""
     javaMap["()"]          = ""
     javaMap["    "]        = "  "
     javaMap["\t"]          = "  "
  }

  Str:Str cshapMap := [Str:Str][:] { ordered = true }
  Void initCshapMap()
  {
     cshapMap["\r\n"]        = "\n"
     cshapMap["Object "]     = "Obj "
     cshapMap["object "]     = "Obj "
     cshapMap["String "]     = "Str "
     cshapMap["string "]     = "Str "
     cshapMap["int "]        = "Int "
     cshapMap["long "]       = "Int "
     cshapMap["double "]     = "Float "
     cshapMap["float "]      = "Float "
     cshapMap["bool "]       = "Bool "
     cshapMap["void "]       = "Void "


     cshapMap["ToString()"]  = "toStr"
     cshapMap[".Equals("]    = ".equals("
     cshapMap[".Count"]      = ".size"
     cshapMap[".Add("]       = ".add("
     cshapMap[".Remove("]    = ".remove("
     cshapMap[".Length"]     = ".siz"

     cshapMap["{ get; set; }"]= ""
     cshapMap[" new "]       = " "
     cshapMap["public "]     = ""
     cshapMap[";"]           = ""
     cshapMap["()"]          = ""
     cshapMap["    "]        = "  "
     cshapMap["\t"]          = "  "
  }

  Str javaTranslate(Str s)
  {
    Str ts := s
    javaMap.each |v,k|
    {
      ts = ts.replace(k, v)
    }
    ts = ts.splitLines.map { it.trimEnd }.join("\n")
    return ts
  }

  Str cshapTranslate(Str s)
  {
    Str ts := s
    cshapMap.each |v,k|
    {
      ts = ts.replace(k, v)
    }
    ts = ts.splitLines.map { it.trimEnd }.join("\n")
    return ts
  }
}